/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambkit.module.cms.rpc.model;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.lambkit.Lambkit;
import com.lambkit.common.service.ServiceKit;
import com.lambkit.common.util.DateTimeUtils;
import com.lambkit.db.sql.column.Column;
import com.lambkit.module.cms.Cms;
import com.lambkit.module.cms.common.CmsConfig;
import com.lambkit.module.cms.core.km.FileStateEnum;
import com.lambkit.module.cms.core.km.FileUtil;
import com.lambkit.module.cms.rpc.api.CmsFileService;
import com.lambkit.module.cms.rpc.model.base.BaseCmsFile;
import com.lambkit.module.cms.rpc.model.sql.CmsFileCriteria;
import com.lambkit.module.cms.rpc.service.impl.CmsFileServiceImpl;
import com.lambkit.module.upms.rpc.model.UpmsUser;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2020-02-23
 * @version 1.0
 * @since 1.0
 */
public class CmsFile extends BaseCmsFile<CmsFile> {

	private static final long serialVersionUID = 1L;
	
	public static CmsFileService service() {
		return ServiceKit.inject(CmsFileService.class, CmsFileServiceImpl.class);
	}
	
	public static CmsFileCriteria sql() {
		return new CmsFileCriteria();
	}
	
	public static CmsFileCriteria sql(Column column) {
		CmsFileCriteria that = new CmsFileCriteria();
		that.add(column);
        return that;
    }

	public CmsFile() {
		CmsConfig config = Lambkit.config(CmsConfig.class);
		String dbconfig = config.getDbconfig();
		if(StrKit.notBlank(dbconfig)) {
			this.use(dbconfig);
		}
	}
	
	public String getCategoryName() {
		return this.get("category");
	}

	public String getUserName() {
		return this.get("username");
	}
	
	public java.lang.String getRealname() {
		return this.get("realname");
	}
    
    public java.lang.String getEmail() {
		return this.get("email");
	}
    public java.lang.Integer getSex() {
		return this.get("sex");
	}
    
    /**
     * 用户头像
     * @return
     */
    public String getAvatar() {
    	return this.get("avatar");
    }

	public String getFileStateStr() {
		return FileStateEnum.stateOf(getFileState()).getStateInfo();
	}

	public String getHiddenedFileName() {
		String str = super.getFileName();
		if (str.length() > 19) {
			//String ext = str.substring(str.lastIndexOf("."), str.length());
			str = str.substring(0, 15)+"...";
		}
		return str;
	}

	public String getFileSizeFormat() {
		if (getFileSize() != null) {
			return FileUtil.getDataSize(getFileSize());
		}
		return "0";
	}

	public void joinUser() {
    	UpmsUser user = UpmsUser.service().findById(this.getFileUserId());
    	if(user!=null) {
    		Long tm = user.getCtime();
    		if(tm!=null) {
    			this.put("utime", DateTimeUtils.longToDate(tm));
    		}
        	user.remove("ctime", "password", "salt");
        	this.put(user);
    	}
    }
	
	public void joinForView() {
    	//String sql = "select count(*) from cms_comment where reftype=0 and refid=?";
    	//Long cnt = Cms.db().queryLong(sql, this.getArticleId());
    	//this.put("comment_num", cnt);
    	StringBuilder sb = new StringBuilder();
    	sb.append("SELECT c.name as category ");
    	sb.append("FROM cms_category where category_id=?");
    	Record r = Cms.db().findFirst(sb.toString(), this.getFileCategoryId());
    	this.put(r);
    }
}
